package com.mhkj.common.util.office;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mics.core.common.util.ftp.FtpUtil;

@Controller
@RequestMapping("/wordController")
public class WordController {


	@RequestMapping(value = "/copyFolder")
	@ResponseBody
	public String copyFolder(String path, String fileName, HttpServletResponse response,HttpServletRequest request)  {

		File upload = null;
		if (fileName != null && !fileName.isEmpty()) {
			String[] fileNameArr = fileName.split(",");
			String[] filePathArr = path.split(",");
			List<String> list = new ArrayList<String>();

			// 循环插入数据库
			for (int i = 0; i < fileNameArr.length; i++) {
				

				try {
				//获取跟目录
				File path1 = new File(ResourceUtils.getURL("classpath:").getPath());
				if(!path1.exists()) path1 = new File("");
				System.out.println("path:"+path1.getAbsolutePath());

				//如果上传目录为/static/images/upload/，则可以如下获取：
				upload= new File(path1.getAbsolutePath(),"upload/");
				if(!upload.exists()) upload.mkdirs();
				System.out.println("upload url:"+upload.getAbsolutePath());
					File testFile = new File(upload.getAbsolutePath()+"/"+fileNameArr[i]);
					System.out.println("Path url:"+testFile.getPath());
					if (!testFile.exists()) {
						InputStream in = FtpUtil.downFile(filePathArr[i], fileNameArr[i], response);
						FileOutputStream output = new FileOutputStream(testFile);
						byte[] b = new byte[1024 * 5];
						int len;
						while ((len = in.read(b)) != -1) {
							output.write(b, 0, len);
						}
						
						output.flush();
						output.close();
						in.close();
					}

					String file1 = testFile.getPath();
					list.add(file1);
				} catch (Exception e) {
					System.out.println("复制整个文件夹内容操作出错");
					e.printStackTrace();

				}
			}
			try {
				new WordUtil4().unitePDF(list, upload.getAbsolutePath()+"/dataPrint.pdf");
				
				
				response.setContentType("multipart/form-data");
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/html");
				// 2.设置文件头：最后一个参数是设置下载文件名
				response.setHeader("Content-Disposition", "attachment;fileName=dataPrint.pdf");
				ServletOutputStream out;
				// 通过文件路径获得File对象
				File html_file = new File(upload.getAbsolutePath()+"/dataPrint.pdf");
				FileInputStream inputStream;
					inputStream = new FileInputStream(html_file);
			
				// 3.通过response获取ServletOutputStream对象(out)
				out = response.getOutputStream();
			
				int b = 0;
				byte[] buffer = new byte[1024];
				while ((b = inputStream.read(buffer)) != -1) {
					// 4.写到输出流(out)中
					out.write(buffer, 0, b);
				}
				inputStream.close();
				out.flush();
				out.close();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return upload.getAbsolutePath()+"\\dataPrint.pdf";
		

	}
}
